﻿using System;
using System.IO;

namespace svnbox {
    public class Monitor {        
        private FileSystemWatcher _watchFiles ;        
        private Svn svn;
        public Monitor(Svn s) {             svn = s;        }

        public static void debug(string s) { System.Diagnostics.Debug.WriteLine("{Monitor} " + s); }

        public bool Start() {
            
            _watchFiles = new FileSystemWatcher();
            _watchFiles.Path = svn.RootPath;
            _watchFiles.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;
            _watchFiles.NotifyFilter = _watchFiles.NotifyFilter | NotifyFilters.Size;
            _watchFiles.IncludeSubdirectories = true;            
            _watchFiles.Changed += new FileSystemEventHandler(HandlerSvnFiles);
            _watchFiles.Created += new FileSystemEventHandler(HandlerSvnFiles);
            _watchFiles.Deleted += new FileSystemEventHandler(HandlerSvnFiles);
            _watchFiles.Renamed += new RenamedEventHandler(HandlerSvnFiles);
            debug("Monitor Up...");

            try {
                _watchFiles.EnableRaisingEvents = true;
                return true;
            } catch (Exception) {
                return false;
            }
        }
        public void Stop() {
            debug("Monitor down...");
            try {
                if (_watchFiles != null) {
                    _watchFiles.EnableRaisingEvents = false;
                    _watchFiles.Dispose();
                }
            } catch { }
            _watchFiles = null;
        }
       
        public  void HandlerSvnFiles(object sender, FileSystemEventArgs e) {
            switch (e.ChangeType) {
                    case WatcherChangeTypes.Changed: SvnChange(e.FullPath); break;
                    case WatcherChangeTypes.Created: SvnCreate(e.FullPath); break;
                    case WatcherChangeTypes.Deleted: SvnDelete(e.FullPath); break;
                    default: break;
            }
        }
        public  void HandlerSvnFiles(object sender, RenamedEventArgs e) {
           if (e.ChangeType == WatcherChangeTypes.Renamed) {
               SvnRename(e.FullPath, e.OldFullPath);
           }            
        }

        public  void SvnChange(string PathFull) {
            if (PathFull.Contains("\\.svn")) { return; }
            debug("Invoke_Changed(Commit)");
            svn.DelaiedCommit("Changed : " + PathFull);
        }
        public  void SvnCreate(string PathFull) {
            if (PathFull.Contains("\\.svn")) { return; }
            debug("Invoke_Add(Add+Commit)");
            svn.doCommand(SvnCommandType.Add, PathFull, false, true);
        }
        public  void SvnDelete(string PathFull) {
            if (PathFull.Contains("\\.svn")) { return;  }
            debug("Invoke_Delete(Delete+Commit)");
            svn.doCommand(SvnCommandType.Delete, PathFull, false, true);
        }
        public  void SvnRename(string PathFull, string PathFullOld) { 
            if (PathFull.Contains("\\.svn")) { return; }
            debug("Invoke_Rename(Remove+Add+Commit)");
            svn.doCommand(SvnCommandType.Rename, PathFull + "|" + PathFullOld, false, true);
        }

    }
}
